package com.ljfiction.book.tools;

import android.util.Log;

import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2016/6/7.
 */
public class TimeTool {
    public static String getTimeAndChange(String string) {
        String str = "";
        if ("".equals(string)) {
            return "";
        }
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date d1 = df.parse(df.format(Long.parseLong(string)));
            Date d2 = new Date();//你也可以获取当前时间
            long diff = d2.getTime() - d1.getTime();//这样得到的差值是微秒级别
            long days = diff / (1000 * 60 * 60 * 24);
            long hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
            long minutes = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);
            if (days > 0) {
                str = df.format(d1);
                str=str.substring(0,16);
            } else if (hours > 0) {
                str = hours + "小时前";
            } else if (minutes > 0) {
                str = minutes + "分钟前";
            } else {
                str = "刚刚";
            }
        } catch (Exception e) {
        }
        return str;

    }

    public static String getTime(String time) {
        String str = "";
        if ("".equals(time)) {
            return "";
        }
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date d1 = format.parse(format.format(Long.parseLong(time)));
            Date d2 = new Date();//获取当前时间
            long diff = d2.getTime() - d1.getTime();//这样得到的差值是豪秒级别
            Log.v("时间差", diff + "");
            long minutes = diff / (60 * 1000);
            long hours = minutes / 60;
            long days = hours / 24;
            long months = days / 30;
            long years = months / 12;
            if (years > 0) {
                str = years + "年之前";
            } else if (months > 0) {
                str = months + "个月之前";
            } else if (days > 0) {
                str = days + "天之前";
            } else if (hours > 0) {
                str = hours + "小时之前";
            } else if (minutes > 0) {
                str = minutes + "分钟之前";
            } else {
                str = "刚刚";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return str;

    }

    public static String getTimeByLongStr(String time) {
        String str = "";
        if ("".equals(time)) {
            return "";
        }
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date d1 = format.parse(format.format(Long.parseLong(time)));
            str = format.format(d1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return str;

    }

    public static String getTimeByLong(Long time) {
        String str = "";
        if ("".equals(time)) {
            return "";
        }
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            Date d1 = format.parse(format.format(time));
            str = format.format(d1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return str;

    }

    /*
    * 获取当前时间
    * */
    public static String getThistime() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        String currentTime = formatter.format(curDate);
        return currentTime;
    }

    public static String getStrByTime(Date time, String type) {
        String str = "";
        String type1 = "yyyy-MM-dd HH:mm:ss";
        if (type != null) {
            type1 = type;
        }
        try {
            SimpleDateFormat format = new SimpleDateFormat(type1);
            str = format.format(time);
            ;
            Log.v("time", format.format(str));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return str;

    }
    public static String getDateTimeMall(long time) {
        String str = "";
        if ("".equals(time)) {
            return "";
        }
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        try {
            str = df.format(df.parse(df.format(time)));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return str;
    }
    public static String getDateTime(long time) {
        String str = "";
        if ("".equals(time)) {
            return "";
        }
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        try {
            str = df.format(df.parse(df.format(time)));
            str = str.substring(5, str.length());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return str;
    }

    public static String getCurrenttime() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        String currentTime = formatter.format(curDate);
        return currentTime;
    }

    public static String StringData() {
        final Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        String mYear = String.valueOf(c.get(Calendar.YEAR)); // 获取当前年份
        String mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 获取当前月份
        String mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH));// 获取当前月份的日期号码
        String mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
        if ("1".equals(mWay)) {
            mWay = "天";
        } else if ("2".equals(mWay)) {
            mWay = "一";
        } else if ("3".equals(mWay)) {
            mWay = "二";
        } else if ("4".equals(mWay)) {
            mWay = "三";
        } else if ("5".equals(mWay)) {
            mWay = "四";
        } else if ("6".equals(mWay)) {
            mWay = "五";
        } else if ("7".equals(mWay)) {
            mWay = "六";
        }
        return mYear + "年" + mMonth + "月" + mDay + "日" + "/星期" + mWay;
    }

    public static int getXinqiji() {
        int i = 0;
        switch (Xinqiji()) {
            case "星期一":
                i = 0;
                break;
            case "星期二":
                i = 1;
                break;
            case "星期三":
                i = 2;
                break;
            case "星期四":
                i = 3;
                break;
            case "星期五":
                i = 4;
                break;
            case "星期六":
                i = 5;
                break;
            case "星期日":
                i = 6;
                break;


        }
        return i;
    }

    public static String Xinqiji() {
        Long ld = null;
        OkHttpUtils.post("http://www.ntsc.ac.cn")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {

                    }
                });
        try {
            URL url = new URL("http://www.ntsc.ac.cn");// 取得资源对象
            URLConnection uc = url.openConnection();// 生成连接对象
            uc.connect();// 发出连接
            ld = uc.getDate();// 读取网站日期时间
            if (ld == null) {
                url = new URL("http://www.beijing-time.org");// 取得资源对象
                uc = url.openConnection();// 生成连接对象
                uc.connect();// 发出连接
                ld = uc.getDate();// 读取网站日期时间
                if (ld == null) {
                    ld = System.currentTimeMillis();
                }
            }
            Date date = new Date(ld);// 转换为标准时间对象
            SimpleDateFormat format = new SimpleDateFormat("EEEE");
            return format.format(date);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (ld == null) {
            ld = System.currentTimeMillis();
        }
        Date date = new Date(ld);// 转换为标准时间对象
        SimpleDateFormat format = new SimpleDateFormat("EEEE");
        return format.format(date);

    }
    /**
     * 获取当月的 天数
     * */
    public static int getCurrentMonthDay() {

        Calendar a = Calendar.getInstance();
        a.set(Calendar.DATE, 1);
        a.roll(Calendar.DATE, -1);
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }

    /**
     * 根据年 月 获取对应的月份 天数
     * */
    public static int getDaysByYearMonth(int year, int month) {

        Calendar a = Calendar.getInstance();
        a.set(Calendar.YEAR, year);
        a.set(Calendar.MONTH, month - 1);
        a.set(Calendar.DATE, 1);
        a.roll(Calendar.DATE, -1);
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }

    /**
     * 根据日期 找到对应日期的 星期
     */
    public static String getDayOfWeekByDate(String date) {
        String dayOfweek = "-1";
        try {
            SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
            Date myDate = myFormatter.parse(date);
            SimpleDateFormat formatter = new SimpleDateFormat("E");
            String str = formatter.format(myDate);
            dayOfweek = str;

        } catch (Exception e) {

        }
        return dayOfweek;
    }
    public static int getremainderDay(){
        Calendar c =  Calendar.getInstance(TimeZone.getTimeZone("GMT+08:OO"));  //这是获得东八区时间，也就是北京时间


        int day = c.get(Calendar.DAY_OF_MONTH);    //获取当前天数

        int last = c.getActualMaximum(c.DAY_OF_MONTH);    //获取本月最大天数
        return last-day;
    }


    
}
